package com.ss.android.article.base.feature.feed.presenter;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Address;
import android.os.Handler;
import android.os.Message;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.text.TextUtils;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.bytedance.frameworks.baselib.network.http.util.HttpQueryHelper;
import com.bytedance.retrofit2.ResponseWrapper;
import com.bytedance.ttnet.a.AppConfig;
import com.bytedance.ttnet.b.RequestParams;
import com.bytedance.ttnet.utils.RetrofitUtils;
import com.ss.android.article.base.IArticleApi;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.RequestValidator;
import com.ss.android.article.base.feature.a.CmdHandler;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.category.a.CategoryManager;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.feature.model.ArticleTip;
import com.ss.android.article.common.e.SubEntrance;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.location.LocationHelper;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.common.util.json.JsonUtil;
import com.ss.android.http.legacy.HttpHeader;
import com.ss.android.http.legacy.a.ResponseHeaders;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.model.AbsArticle;
import com.ss.android.newmedia.BaseApplication;
import com.ss.android.newmedia.e.InstalledAppTracker2;
import com.ss.android.newmedia.util.AppUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.EOFException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import inline.TextDemo;

public final class ArticleQueryThread extends AbsApiThread {
    private static final AtomicInteger a = new AtomicInteger();
    private static final AtomicInteger c = new AtomicInteger();
    private static final List h = new ArrayList();
    private static final int[] n = new int[]{-1, -1, -1};
    private static int b = 3;
    private static volatile int d = 0;
    private static volatile int articleRecentAppSentCnt = -1;
    private static List f = null;
    private static List g = null;
    private static AtomicLong m = new AtomicLong();

    static {
        h.add("ib.snssdk.com");
    }

    private final Context mContext;
    private final Handler mHandler;
    private final ArticleQueryObj mArticleQueryObj;
    private final int[] l;

    public ArticleQueryThread(Context context, Handler handler, ArticleQueryObj articleQueryObj) {
        super(Priority.IMMEDIATE);
        this.mContext = context.getApplicationContext();
        this.mHandler = handler;
        this.mArticleQueryObj = articleQueryObj;
        this.l = articleQueryObj.categoryType == 1 ? a(context) : null;
        CategoryManager.getInstance(context);
    }

    public static void a(List arg0) {
        f = arg0;
    }

    public static void a(int arg0) {
        b = arg0;
    }

    public static void requestArticleTip(Context context, ArticleTip articleTip) throws Throwable {
        if (articleTip != null) {
            if (android.support.design.a.isTextEmpty(articleTip.category)) {
                articleTip.validCategory = false;
            } else {
                HttpQueryHelper httpQueryHelper = new HttpQueryHelper();
                boolean isAllCategory = "__all__".equals(articleTip.category);
                boolean isNewsLocal = articleTip.category.startsWith("news_local");
                if (!android.support.design.a.isTextEmpty(articleTip.category) && !isAllCategory) {
                    httpQueryHelper.put("category", isNewsLocal ? "news_local" : articleTip.category);
                }

                if (articleTip.minBehotTime > 0) {
                    httpQueryHelper.put("min_behot_time", articleTip.minBehotTime);
                }

                Address address = LocationHelper.getInstance(context).loadSavedLocation();
                if (address != null && (address.hasLatitude()) && (address.hasLongitude())) {
                    String city = address.getLocality();
                    if (!android.support.design.a.isTextEmpty(city)) {
                        httpQueryHelper.put("city", city);
                    }
                }

                if (isNewsLocal && !android.support.design.a.isTextEmpty(articleTip.userCity)) {
                    httpQueryHelper.put("user_city", articleTip.userCity);
                }

                String response = RetrofitUtils
                        .createService("http://ib.snssdk.com", IArticleApi.class)
                        .articleTip(-1, httpQueryHelper.queryMap)
                        .execute().body;
                articleTip.validCategory = false;
                if (android.support.design.a.isTextEmpty(response)) {
                    return;
                }

                JSONObject jsonObject = new JSONObject(response);
                if (!isApiSuccess(jsonObject)) {
                    Logger.w("ArticleQueryThread", "query category tip error: " + response);
                    return;
                }

                articleTip.validCategory = true;
                JSONObject data = jsonObject.optJSONObject("data");
                if (data == null) {
                    return;
                }
                articleTip.tip = data.optString("tip");
                int style = data.optInt("style");
                int count = data.optInt("count", -1);
                if (style == 1 && count > 0) {
                    articleTip.count = ".";
                    return;
                }
                if (count > 99) {
                    articleTip.count = "···";
                    return;
                }
                if (count <= 0) {
                    return;
                }
                articleTip.count = String.valueOf(count);
            }
        }
    }

    public static ArticleDetail getArticleDetail(
            DBHelper dbHelper,
            AbsArticle absArticle,
            boolean arg22,
            String ifNoneMatch) throws Throwable {
        String response;
        int v2_5;
        String url;
        long v2_3;
        if (absArticle == null) {
            return null;
        }

        List v2_1 = g;
        if (arg22) {
            v2_1 = f;
        }

        if (v2_1 == null || (v2_1.isEmpty())) {
            v2_1 = h;
        }

        int v10 = v2_1.size();
        int v3 = 0;
        Iterator v11 = v2_1.iterator();
        while (true) {
            if (!v11.hasNext()) {
                return null;
            }

            Object v2_2 = v11.next();
            int v9 = v3 + 1;
            if (v9 > 3) {
                return null;
            }

            StringBuilder stringBuilder = new StringBuilder("http://");
            stringBuilder.append(((String) v2_2));
            if (arg22) {
                stringBuilder.append("/article/full/lite/14/1/");
            } else {
                stringBuilder.append("/article/content/lite/14/1/");
            }

            stringBuilder
                    .append(absArticle.groupId)
                    .append("/")
                    .append(absArticle.itemId)
                    .append("/")
                    .append(absArticle.aggrType)
                    .append("/");
            if (arg22) {
                v2_3 = CmdHandler.a(new Article(absArticle.groupId, absArticle.itemId, absArticle.aggrType).getItemKey());
                if (v2_3 < 0) {
                    v2_3 = 0;
                }
                stringBuilder.append(v2_3).append("/");
            }
            url = stringBuilder.toString();
            ResponseHeaders responseHeaders = new ResponseHeaders();
            ArrayList httpHeaderList = null;
            if (!arg22 && !android.support.design.a.isTextEmpty(ifNoneMatch)) {
                httpHeaderList = new ArrayList();
                NetworkUtils.addCacheValidationHeaders(httpHeaderList, ifNoneMatch);
            }

            try {
                long startTs = System.currentTimeMillis();
                RequestParams requestContext = new RequestParams();
                if (v9 < 3 && v9 < v10 && !NetworkUtils.is2g(BaseApplication.getApplication().getApplicationContext())) {
                    if (AppData.getInstance().bd == 1) {
                        v2_5 = 1;
                    } else {
                        v2_5 = 0;
                    }

                    if (v2_5 != 0) {
                        requestContext.timeout_connect = 7000;
                        requestContext.timeout_write = 7000;
                        requestContext.timeout_read = 7000;
                    }
                }
                requestContext.cdn_request_num = v9;
                response = NetworkUtils.doGet(-1, url, true, httpHeaderList, responseHeaders, requestContext);
                if (System.currentTimeMillis() - startTs > 3000) {
                    reportEvent("loading", absArticle.groupId, null);
                }
            } catch (Exception exception) {
                if ((exception instanceof HttpResponseException)) {
                    int responseStatusCode = ((HttpResponseException) exception).getStatusCode();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("error_type", 0);
                    jsonObject.put("status", responseStatusCode);
                    reportEvent("error", absArticle.groupId, jsonObject);
                    if (responseStatusCode == 304
                            && !arg22
                            && !android.support.design.a.isTextEmpty(ifNoneMatch)) {
                        long ts = System.currentTimeMillis();
                        HttpHeader etagHttpHeader = responseHeaders.getHeader("ETag");
                        String etag = null;
                        if (etagHttpHeader != null) {
                            etag = etagHttpHeader.value();
                        }

                        long maxAge = NetworkUtils.extractMaxAge(responseHeaders);
                        if (maxAge < 0) {
                            maxAge = 0;
                        }
                        if (dbHelper != null) {
                            dbHelper.updateDetail(absArticle.groupId, absArticle.itemId, absArticle.aggrType, etag, ts, maxAge);
                        }
                        if (Logger.debug()) {
                            Logger.v("ArticleQueryThread", "item detail get 304 " + absArticle.groupId + " " + ifNoneMatch);
                        }
                        return null;
                    }
                }
                v3 = v9;
                continue;
            }

            if (android.support.design.a.isTextEmpty(response)) {
                JSONObject jo = new JSONObject();
                jo.put("error_type", 1);
                jo.put("error_msg", "missing response");
                reportEvent("error", absArticle.groupId, jo);
                v3 = v9;
                continue;
            }

            try {
                JSONObject responseJO = new JSONObject(response);
                if (!isApiSuccess(responseJO)) {
                    Logger.w("ArticleQueryThread", "get item detail error: " + response);
                    responseJO = new JSONObject();
                    responseJO.put("error_type", 1);
                    responseJO.put("error_msg", "api message error");
                    reportEvent("error", absArticle.groupId, responseJO);
                    v3 = v9;
                    continue;
                }

                JSONObject dataJO = responseJO.getJSONObject("data");
                long groupId = dataJO.optLong("group_id");
                if (absArticle.groupId != groupId) {
                    Logger.d("ArticleQueryThread", "detail error: group_id unmatch: " + absArticle.groupId + " " + groupId);
                    v3 = v9;
                    continue;
                }
                boolean delete = dataJO.optInt("delete") > 0;
                String content = dataJO.optString("content");
                long ts = System.currentTimeMillis();
                ArticleDetail articleDetail = new ArticleDetail();
                articleDetail.itemId = absArticle.groupId;
                articleDetail.content = content;
                articleDetail.cacheToken = ifNoneMatch;
                articleDetail.timestamp = ts;
                articleDetail.delete = delete;
                String image_detail = "";
                String thumb_image = "";
                JSONArray imageDetailJA = dataJO.optJSONArray("image_detail");
                if (imageDetailJA != null) {
                    try {
                        articleDetail.imageDetail = ImageInfo.parseImageList(imageDetailJA, false);
                        image_detail = imageDetailJA.toString();
                    } catch (Exception exception) {
                    }
                }

                JSONArray thumbImageJA = dataJO.optJSONArray("thumb_image");
                if (thumbImageJA != null) {
                    try {
                        ImageInfo.parseImageList(thumbImageJA, false);
                        thumb_image = thumbImageJA.toString();
                    } catch (Exception exception) {
                    }
                }
                articleDetail.appendExtraData(dataJO);
                int articleType = dataJO.optInt("article_type");
                int v5_4 = (articleDetail.itemId & 131072) <= 0 || articleType != 1 ? 0 : 1;
                if (!articleDetail.delete
                        && (android.support.design.a.isTextEmpty(content))
                        && v5_4 == 0) {
                    if (articleDetail.galleryImageInfoList == null || articleDetail.galleryImageInfoList.isEmpty()) {
                        v3 = v9;
                        continue;
                    }
                }
                Article article = new Article(absArticle.groupId, absArticle.itemId, absArticle.aggrType);
                if (arg22) {
                    JsonUtil.parseJSONObject(dataJO, article);
                    article.delete = delete;
                    if (article.delete) {
                        article.commentCount = 0;
                    } else {
                        ArrayList<CellRef> arrayList = new ArrayList();
                        arrayList.add(new CellRef("", article.behotTime, article));
                        dbHelper.insertArticleList(arrayList);
                    }
                    articleDetail.article = article;
                }

                if (articleDetail.delete) {
                    dbHelper.deleteArticle(article);
                    reportEvent("delete", absArticle.groupId, null);
                } else {
                    HttpHeader etagHeader = responseHeaders.getHeader("ETag");
                    String etag = null;
                    if (etagHeader != null) {
                        etag = etagHeader.value();
                    }
                    long maxAge = NetworkUtils.extractMaxAge(responseHeaders);
                    if (maxAge < 0) {
                        maxAge = 0;
                    }
                    dbHelper.insertDetail(article, articleDetail, etag, image_detail, thumb_image, maxAge);
                }

                if (articleType == 1) {
                    if (articleDetail.article == null || android.support.design.a.isTextEmpty(articleDetail.article.articleUrl)) {
                        JSONObject jo = new JSONObject();
                        jo.put("error_type", 1);
                        jo.put("error_msg", "webContent with no content");
                        reportEvent("error", absArticle.groupId, jo);
                        return articleDetail;
                    }
                }
                if (articleType != 0) {
                    return articleDetail;
                }

                if (!android.support.design.a.isTextEmpty(articleDetail.content)) {
                    return articleDetail;
                }

                if (articleDetail.imageDetail != null && !articleDetail.imageDetail.isEmpty()) {
                    return articleDetail;
                }

                JSONObject jo = new JSONObject();
                jo.put("error_type", 1);
                if (android.support.design.a.isTextEmpty(articleDetail.content)) {
                    jo.put("error_msg", "nativeArticle with no content");
                } else {
                    jo.put("error_msg", "nativeGallery with no gallery");
                }
                reportEvent("error", absArticle.groupId, jo);
                return articleDetail;
            } catch (JSONException jsonException) {
                try {
                    jsonException.printStackTrace();
                    a(null, url, response);
                    a(url, response);
                    v3 = v9;
                    continue;
                } catch (Exception exception) {
                    JSONObject jo = new JSONObject();
                    jo.put("error_type", 1);
                    jo.put("error_msg", "missing field data");
                    reportEvent("error", absArticle.groupId, jo);
                    v3 = v9;
                    continue;
                }
            }
        }
    }

    public static ArticleInfo getArticleInfo(DBHelper dbHelper,
                                             Article article,
                                             long ad_id,
                                             String from_category,
                                             String from,
                                             int flags,
                                             int article_page,
                                             boolean useV16) throws Throwable {
        ArticleInfo articleInfo;
        if (article == null) {
            return null;
        } else {
            String articleInformationUrl = useV16 ? ArticleConstants.articleInformationUrlV16
                    : ArticleConstants.articleInformationUrlV14;
            UrlBuilder urlBuilder = new UrlBuilder(articleInformationUrl);
            urlBuilder.addParam("group_id", article.groupId);
            urlBuilder.addParam("item_id", article.itemId);
            urlBuilder.addParam("aggr_type", article.aggrType);
            urlBuilder.addParam("context", 1);
            if (!android.support.design.a.isTextEmpty(from)) {
                urlBuilder.addParam("from", from);
            }
            if (ad_id > 0) {
                urlBuilder.addParam("ad_id", ad_id);
            }

            if (flags > 0) {
                urlBuilder.addParam("flags", flags);
            }

            if (article.videoSubjectId > 0) {
                urlBuilder.addParam("video_subject_id", article.videoSubjectId);
            }

            if (!android.support.design.a.isTextEmpty(from_category)) {
                if (from_category.startsWith("news_local_")) {
                    from_category = "news_local";
                }
                urlBuilder.addParam("from_category", from_category);
            }

            urlBuilder.addParam("article_page", article_page);
            String response = NetworkUtils.doGet(-1, urlBuilder.build());
            if (android.support.design.a.isTextEmpty(response)) {
                return null;
            }

            JSONObject jsonObject = new JSONObject(response);
            if (!isApiSuccess(jsonObject)) {
                Logger.d("ArticleQueryThread", "get article info error: " + jsonObject);
                return null;
            }

            JSONObject dataJO = jsonObject.getJSONObject("data");
            articleInfo = new ArticleInfo(article.groupId, article.itemId, (byte) 0);
            articleInfo.a(dataJO, useV16);
            if (!articleInfo.delete) {
                return articleInfo;
            }

            article.delete = true;
            if (dbHelper != null) {
                dbHelper.deleteArticle(article);
            } else {
                return articleInfo;
            }
        }

        return articleInfo;
    }

    protected static void reportEvent(String arg9, long arg10, JSONObject arg12) {
        if (!android.support.design.a.isTextEmpty(arg9)) {
            MobClickCombiner.onEvent(BaseApplication.getApplication(), "article", "detail_load", arg9, arg10, 0, arg12);
        }
    }

    protected static void a(Context context, String arg10, String arg11) {
        Context v1;
        if (!android.support.design.a.isTextEmpty(arg11)) {
            if (context == null) {
                v1 = AppData.getInstance().getAppContext().getContext();
            } else {
                v1 = context;
            }

            try {
                JSONObject v8 = new JSONObject();
                if (!android.support.design.a.isTextEmpty(arg10)) {
                    v8.put("url", arg10);
                }

                byte[] v0_1 = arg11.getBytes("UTF-8");
                v8.put("data", com.bytedance.common.utility.aa.a(v0_1, Math.min(v0_1.length, 16)));
                MobClickCombiner.onEvent(v1, "api_error", "json", 0, 0, v8);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    protected static void a(String arg5, String arg6) {
        try {
            JSONObject v0_1 = new JSONObject();
            if (!android.support.design.a.isTextEmpty(arg5)) {
                v0_1.put("url", arg5);
            }

            byte[] v1 = arg6.getBytes("UTF-8");
            v0_1.put("data", com.bytedance.common.utility.aa.a(v1, Math.min(v1.length, 16)));
        } catch (Exception exception) {
        }
    }

    @SuppressLint("MissingPermission")
    private static int[] a(Context arg9) {
        CellLocation v0_2;
        int v0_1;
        int v2_1;
        Throwable v3_1;
        int v3 = 0;
        int v1 = -1;
        long v2 = System.currentTimeMillis();
        if (v2 - m.get() <= 600000) {
        } else {
            m.set(v2);
            try {
                v3 = ToolUtils.getLocationMode(arg9);
                v0_2 = ((TelephonyManager) arg9.getSystemService(Context.TELEPHONY_SERVICE)).getCellLocation();
                if (!(v0_2 instanceof GsmCellLocation)) {
                    if ((v0_2 instanceof CdmaCellLocation)) {
                        v2_1 = ((CdmaCellLocation) v0_2).getNetworkId();
                        v0_1 = ((CdmaCellLocation) v0_2).getBaseStationId();
                        v1 = v2_1;
                    } else {
                        v0_1 = v1;
                    }
                    v2_1 = v3;
                } else {
                    v2_1 = ((GsmCellLocation) v0_2).getLac();
                    v0_1 = ((GsmCellLocation) v0_2).getCid();
                    v1 = v2_1;
                    v2_1 = v3;
                }
            } catch (Throwable v0) {
                v2_1 = v3;
                v3_1 = v0;
                v0_1 = v1;
                Logger.w("ArticleQueryThread", "get loc info exception: " + v3_1);
                int v8_1 = v1;
                v1 = v0_1;
                v0_1 = v8_1;
            }

            int[] v3_2 = n;
            synchronized (v3_2) {
                n[0] = v2_1;
                if (v1 >= 0) {
                    n[1] = v1;
                }

                if (v0_1 >= 0) {
                    n[2] = v0_1;
                }
            }

            if (Logger.debug()) {
                Logger.d("ArticleQueryThread", "loc info " + v2_1 + " " + v1 + " " + v0_1);
            }
        }

        int[] v1_1 = n;
        synchronized (v1_1) {
            int[] v0_3 = new int[3];
            System.arraycopy(n, 0, v0_3, 0, 3);
            return v0_3;
        }
    }

    private static void parseSubEntranceList(Context context, ArticleQueryObj articleQueryObj, JSONObject jsonObject) {
        SharedPreferences.Editor editor;
        if (context != null
                && articleQueryObj != null
                && articleQueryObj.categoryType == 1) {
            if (jsonObject.isNull("sub_entrance_list")) {
                articleQueryObj.T = true;
            } else {
                articleQueryObj.T = false;
                JSONArray subEntranceJa = jsonObject.optJSONArray("sub_entrance_list");
                if (subEntranceJa != null) {
                    editor = context.getSharedPreferences("app_setting", 0).edit();
                    editor.putString(articleQueryObj.category + "sub_channel", subEntranceJa.toString());
                    SharedPreferenceCompat.commit(editor);
                    editor = context.getSharedPreferences("app_setting", 0).edit();
                    if (subEntranceJa.length() > 0) {
                        editor.putLong(articleQueryObj.category + "sub_channel_time", System.currentTimeMillis());
                    } else {
                        editor.putLong(articleQueryObj.category + "sub_channel_time", 0);
                    }
                    SharedPreferenceCompat.commit(editor);
                    ArrayList<SubEntrance> subEntranceList = new ArrayList();
                    for (int i = 0; i < subEntranceJa.length(); i++) {
                        JSONObject jo = subEntranceJa.optJSONObject(i);
                        SubEntrance subEntrance = new SubEntrance();
                        subEntrance.initWithJO(jo);
                        subEntranceList.add(subEntrance);
                    }
                    articleQueryObj.subEntranceList.clear();
                    articleQueryObj.subEntranceList.addAll(subEntranceList);
                }
            }
        }
    }

    private static boolean a(Context context, ArticleQueryObj articleQueryObj) {
        String v5;
        long[] v7;
        boolean[] v6;
        boolean v0_2;
        int v0_1;
        int v12 = 2;
        try {
            DBHelper dbHelper = DBHelper.getInstance(context);
            if (("widget".equals(articleQueryObj.ttFrom)) || ("widget_m".equals(articleQueryObj.ttFrom))) {
                v0_1 = 1;
            } else {
                v0_1 = 0;
            }
            int v2 = articleQueryObj.categoryType != 1 || !"__all__".equals(articleQueryObj.category) || v0_1 == 0 ? 0 : 1;
            v0_2 = "permanent_notify".equals(articleQueryObj.ttFrom);
            v0_1 = articleQueryObj.categoryType != 1 || !"__all__".equals(articleQueryObj.category) || !v0_2 ? 0 : 1;
            articleQueryObj.T = true;
            v6 = new boolean[1];
            v7 = new long[1];
            if (!articleQueryObj.d && !articleQueryObj.j) {

            } else {
                if (articleQueryObj.categoryType == v12) {
                    articleQueryObj.q = dbHelper.queryFavor(articleQueryObj.maxBehotTime, articleQueryObj.count);
                    articleQueryObj.v = true;
                    return true;
                }

                if (articleQueryObj.categoryType != 1 && articleQueryObj.categoryType != 7) {
                } else {

                    v5 = articleQueryObj.category;
                    if ("news_local".equals(v5)) {
                        v5 = articleQueryObj.category + "_" + articleQueryObj.userCity;
                    }

                    if (v2 != 0) {
                        v5 = "__widget__";
                    } else {
                        if (v0_1 != 0) {
                            v5 = "__permanent__";
                        }
                    }


                    List v2_1 = dbHelper.queryRecent(articleQueryObj.maxBehotTime, articleQueryObj.count, v5, v6, v7);
                    if (v2_1 == null || v2_1.isEmpty()) {

                    } else {
                        articleQueryObj.v = true;
                        articleQueryObj.firstCateBehotTime = ((CellRef) v2_1.get(0)).cateBehotTime;
                        articleQueryObj.lastCateBehotTime = ((CellRef) v2_1.get(v2_1.size() - 1)).cateBehotTime;
                        articleQueryObj.q = v2_1;
                        articleQueryObj.t = v6[0];
                        articleQueryObj.u = v7[0];
                        AppData v0_3 = AppData.getInstance();
                        String v3 = articleQueryObj.userCity;
                        if (v0_3.mContext == null || (android.support.design.a.isTextEmpty(v3))) {
                            v0_1 = 0;
                        } else {
                            v0_1 = v0_3.mContext.getSharedPreferences("app_setting", 0).getInt(v3, 0);
                        }
                        articleQueryObj.feedFlag = v0_1;
                        Iterator v2_2 = v2_1.iterator();
                        while (v2_2.hasNext()) {
                            Object v0_4 = v2_2.next();
                            if (((CellRef) v0_4).stickStyle <= 0) {
                                continue;
                            }

                            if (articleQueryObj.s == null) {
                                articleQueryObj.s = new ArrayList();
                            }

                            articleQueryObj.s.add(v0_4);
                        }

                        v0_2 = AppData.getInstance().D();
                        if ((v0_2) && articleQueryObj.refer == 1) {
                            articleQueryObj.showEtStatus = dbHelper.queryCategoryShowEtStatus(v5, articleQueryObj.refer);
                            articleQueryObj.postContentHint = dbHelper.queryPostContentHint(v5, articleQueryObj.refer);
                        } else if (v0_2 && articleQueryObj.refer == v12) {
                            dbHelper.queryCategoryConcernInfo(String.valueOf(articleQueryObj.concernId), articleQueryObj.refer);
                            articleQueryObj.showEtStatus = dbHelper.queryCategoryShowEtStatus(String.valueOf(articleQueryObj.concernId), articleQueryObj.refer);
                            articleQueryObj.postContentHint = dbHelper.queryPostContentHint(String.valueOf(articleQueryObj.concernId), articleQueryObj.refer);
                        }
                        articleQueryObj.E = articleQueryObj.concernId;
                        return true;
                    }
                }
            }
        } catch (Throwable throwable) {

        }

        if (articleQueryObj.d) {
            articleQueryObj.v = true;
            return true;
        }

        return false;
    }

    private static boolean queryArticle(Context context, ArticleQueryObj articleQueryObj, int[] locModes) {
        boolean v14_3;
        JSONObject v8_2;
        Throwable v14_2;
        long v32;
        Throwable v11_2;
        int v16;
        int v12;
        int v11_1;
        boolean v15;
        int retryCount;
        String v11;
        String fullUrl;
        int lac;
        long v10_1;
        String v9_1;
        JSONObject v9;
        int v19;

        int useOkHttp = AppData.useOkHttp();
        if (c.get() < 2) {
            useOkHttp = useOkHttp > 0 ? 1 : 0;
        } else {
            useOkHttp = useOkHttp == 2 ? 1 : 0;
        }
        DBHelper dbHelper = DBHelper.getInstance(context);
        boolean fromWidget = ("widget".equals(articleQueryObj.ttFrom)) || ("widget_m".equals(articleQueryObj.ttFrom));
        int v22 = articleQueryObj.categoryType != 1
                || !"__all__".equals(articleQueryObj.category)
                || !fromWidget ? 0 : 1;
        boolean isPermanentNotify = "permanent_notify".equals(articleQueryObj.ttFrom);
        int v21 = articleQueryObj.categoryType != 1
                || !"__all__".equals(articleQueryObj.category)
                || !isPermanentNotify ? 0 : 1;
        HttpQueryHelper httpQueryHelper = new HttpQueryHelper();
        String path;
        String website = null;
        int v5 = 0;
        try {
            if (articleQueryObj.categoryType == 2) {//favorites
                website = "http://isub.snssdk.com";
                path = "/2/data/v46/favorites/";
                httpQueryHelper.put("order", "desc");
                if (articleQueryObj.refreshReason >= 0) {
                    httpQueryHelper.put("refresh_reason", articleQueryObj.refreshReason);
                }
                if (articleQueryObj.maxBehotTime > 0) {
                    httpQueryHelper.put("max_repin_time", articleQueryObj.maxBehotTime);
                }
                if (articleQueryObj.count > 0) {
                    httpQueryHelper.put("count", articleQueryObj.count);
                }
                v19 = 0;
            } else {
                if (articleQueryObj.categoryType != 8
                        && articleQueryObj.categoryType != 9) {
                    if (articleQueryObj.categoryType == 5) {//video
                        website = "http://ib.snssdk.com";
                        path = "/vertical/video/1/";
                        httpQueryHelper.put("offset", articleQueryObj.offset);
                        httpQueryHelper.put("concern_id", articleQueryObj.concernId);
                        if (articleQueryObj.refreshReason >= 0) {
                            httpQueryHelper.put("refresh_reason", articleQueryObj.refreshReason);
                        }

                        if (articleQueryObj.minBehotTime > 0) {
                            httpQueryHelper.put("min_behot_time", articleQueryObj.minBehotTime);
                        }

                        if (articleQueryObj.maxBehotTime > 0) {
                            httpQueryHelper.put("max_behot_time", articleQueryObj.maxBehotTime);
                        }

                        if (!android.support.design.a.isTextEmpty(articleQueryObj.jsonQuery)) {
                            JSONObject queryJO = new JSONObject(articleQueryObj.jsonQuery);
                            Iterator<String> iterator = queryJO.keys();
                            while (iterator.hasNext()) {
                                httpQueryHelper.put(iterator.next(), queryJO.optString(iterator.next()));
                            }
                        }
                        v19 = 0;
                    } else {
                        if (articleQueryObj.categoryType == 1
                                || articleQueryObj.categoryType == 4) {
                            website = "http://ib.snssdk.com";
                            path = "/api/news/feed/v46/";
                            isPermanentNotify = "__all__".equals(articleQueryObj.category);
                            if (articleQueryObj.categoryType == 4) {
                                httpQueryHelper.put("entry_id", 0);
                            } else {
                                if (!android.support.design.a.isTextEmpty(articleQueryObj.category)
                                        && !isPermanentNotify
                                        && !articleQueryObj.category.equals(String.valueOf(articleQueryObj.concernId))) {
                                    httpQueryHelper.put("category", articleQueryObj.category);
                                }
                                if (articleQueryObj.concernId > 0) {
                                    httpQueryHelper.put("concern_id", articleQueryObj.concernId);
                                }

                                if (articleQueryObj.movieId > 0) {
                                    httpQueryHelper.put("movie_id", articleQueryObj.movieId);
                                }

                                httpQueryHelper.put("refer", articleQueryObj.refer);
                            }

                            if (articleQueryObj.refreshReason >= 0) {
                                httpQueryHelper.put("refresh_reason", articleQueryObj.refreshReason);
                            }

                            httpQueryHelper.put("count", articleQueryObj.count);
                            if (!android.support.design.a.isTextEmpty(articleQueryObj.extra)) {
                                httpQueryHelper.put("extra", articleQueryObj.extra);
                            }

                            if (articleQueryObj.minBehotTime > 0) {
                                httpQueryHelper.put("min_behot_time", articleQueryObj.minBehotTime);
                            }

                            if (articleQueryObj.maxBehotTime > 0) {
                                httpQueryHelper.put("max_behot_time", articleQueryObj.maxBehotTime);
                            }

                            ArticleQueryObj.CtrlFlag ctrlFlag = ArticleQueryObj.CtrlFlag.onVideoTab;
                            int flag;
                            if (ctrlFlag == null) {
                                flag = 0;
                            } else if (articleQueryObj.X == null) {
                                flag = 0;
                            } else if (articleQueryObj.X.contains(ctrlFlag)) {
                                flag = 1;
                            } else {
                                flag = 0;
                            }

                            if (flag != 0) {
                                AppData.getInstance();
                            }

                            httpQueryHelper.put("last_refresh_sub_entrance_interval",
                                    (System.currentTimeMillis() - context.getSharedPreferences("app_setting", 0)
                                            .getLong(articleQueryObj.category + "sub_channel_time", 0)) / 1000);
                            LocationHelper locationHelper = LocationHelper.getInstance(context);
                            Address address = locationHelper.loadSavedLocation();
                            long locTime = locationHelper.getLocTime();
                            if (locModes != null && locModes.length > 0 && locModes[0] >= 0) {
                                httpQueryHelper.put("loc_mode", locModes[0]);
                            }

                            if (address != null
                                    && (address.hasLatitude())
                                    && (address.hasLongitude())) {
                                if (locTime > 0) {
                                    locTime /= 1000;
                                }
                                if (locTime > 0) {
                                    httpQueryHelper.put("loc_time", locTime);
                                }

                                httpQueryHelper.put("latitude", address.getLatitude());
                                httpQueryHelper.put("longitude", address.getLongitude());
                                String city = address.getLocality();
                                if (!android.support.design.a.isTextEmpty(city)) {
                                    httpQueryHelper.put("city", city);
                                }
                            }

                            if (!android.support.design.a.isTextEmpty(articleQueryObj.ttFrom)) {
                                httpQueryHelper.put("tt_from", articleQueryObj.ttFrom);
                            }

                            if (("news_local".equals(articleQueryObj.category)) && !android.support.design.a.isTextEmpty(articleQueryObj.userCity)) {
                                httpQueryHelper.put("user_city", articleQueryObj.userCity);
                            }

                            if (locModes != null) {
                                if (locModes == null) {
                                    lac = -1;
                                } else if (locModes.length > 1) {
                                    lac = locModes[1];
                                } else {
                                    lac = -1;
                                }
                                int cid;
                                if (locModes == null) {
                                    cid = -1;
                                } else if (locModes.length > 2) {
                                    cid = locModes[2];
                                } else {
                                    cid = -1;
                                }
                                if (lac >= 0) {
                                    httpQueryHelper.put("lac", lac);
                                }

                                if (cid >= 0) {
                                    httpQueryHelper.put("cid", cid);
                                }
                            }

                            if (articleRecentAppSentCnt < 0) {
                                articleRecentAppSentCnt = 0;
                                int articleRecentAppSentCntTmp = context.getSharedPreferences("misc_config", 0)
                                        .getInt("article_recent_app_sent_cnt", 0);
                                if (articleRecentAppSentCntTmp > 0) {
                                    articleRecentAppSentCnt = articleRecentAppSentCntTmp;
                                }
                            }

                            if (articleRecentAppSentCnt <= 0 && d < 2) {
                                String recent_apps = InstalledAppTracker2.getRecentApps(context);
                                if (recent_apps != null) {
                                    httpQueryHelper.put("recent_apps", recent_apps);
                                    v5 = 1;
                                    ++d;
                                }
                            }
                            httpQueryHelper.put("cp", TextDemo.a());
                            v19 = v5;
                        } else {

                            if (articleQueryObj.categoryType == 3) {//search
                                if (android.support.design.a.isTextEmpty(articleQueryObj.category)) {
                                    articleQueryObj.exceptionMsg = "query params category is null.";
                                    return false;
                                } else {
                                    website = "http://ib.snssdk.com";
                                    path = "/2/article/v46/search/";
                                    httpQueryHelper.put("keyword", articleQueryObj.category);
                                    httpQueryHelper.put("offset", articleQueryObj.offset);
                                    httpQueryHelper.put("count", articleQueryObj.count);
                                    if (!android.support.design.a.isTextEmpty(articleQueryObj.ttFrom)) {
                                        httpQueryHelper.put("from", articleQueryObj.ttFrom);
                                    }
                                    v19 = 0;
                                }
                            } else if (articleQueryObj.categoryType == 7) {//feed
                                website = "http://ib.snssdk.com";
                                switch (articleQueryObj.refer) {
                                    case 0:
                                        path = "/wenda/v1/native/feedbrow/";
                                        if (!android.support.design.a.isTextEmpty(articleQueryObj.category)) {
                                            httpQueryHelper.put("category", articleQueryObj.category);
                                        }
                                        break;
                                    case 1:
                                        path = null;
                                        break;
                                    case 2:
                                        path = "/wenda/v1/concern/brow/?version=40";
                                        if (articleQueryObj.concernId > 0) {
                                            httpQueryHelper.put("concern_id", articleQueryObj.concernId);
                                        }
                                        break;
                                    default:
                                        path = null;
                                        break;
                                }

                                if (TextUtils.isEmpty((path))) {
                                    return false;
                                } else {
                                    httpQueryHelper.put("wd_version", "1");
                                    if (!TextUtils.isEmpty(articleQueryObj.apiParam)) {
                                        httpQueryHelper.put("api_param", articleQueryObj.apiParam);
                                    }

                                    if (articleQueryObj.refreshReason >= 0) {
                                        httpQueryHelper.put("refresh_reason", articleQueryObj.refreshReason);
                                    }

                                    if (articleQueryObj.count > 0) {
                                        httpQueryHelper.put("count", articleQueryObj.count);
                                    }

                                    if (articleQueryObj.minBehotTime > 0) {
                                        httpQueryHelper.put("min_behot_time", articleQueryObj.minBehotTime);
                                    }

                                    if (articleQueryObj.maxBehotTime > 0) {
                                        httpQueryHelper.put("max_behot_time", articleQueryObj.maxBehotTime);
                                    }
                                    v19 = 0;
                                }
                            } else {
                                return false;
                            }
                        }
                    }
                } else {
                    website = "http://isub.snssdk.com";
                    path = "/2/data/v46/history/list";
                    httpQueryHelper.put("order", "desc");
                    if (articleQueryObj.maxBehotTime > 0) {
                        httpQueryHelper.put("max_time", articleQueryObj.maxBehotTime);
                    }

                    if (articleQueryObj.count > 0) {
                        httpQueryHelper.put("count", articleQueryObj.count);
                    }

                    if (!TextUtils.isEmpty(articleQueryObj.historyType)) {
                        httpQueryHelper.put("history_type", articleQueryObj.historyType);
                    }
                    v19 = 0;
                }
            }
        } catch (Exception e) {
            if (Logger.debug()) {
                e.printStackTrace();
            }
            v19 = 0;
            path = null;
            website = null;
        }
        int v6_4;
        int v7_1;
        try {
            httpQueryHelper.website = website + path;
            fullUrl = httpQueryHelper.getEncodeQuery();
            v11 = null;
            articleQueryObj.startRequestTimeStamp = System.currentTimeMillis();
            v9 = null;
            long parseStartTime = System.currentTimeMillis();
            IArticleApi articleApi;
            if (useOkHttp != 0) {
                articleApi = RetrofitUtils.createService2(website, IArticleApi.class);
            } else {
                articleApi = RetrofitUtils.createService(website, IArticleApi.class);
            }
            retryCount = 0;
            v15 = false;
            isPermanentNotify = false;
            String host = fullUrl;
            String v10_2 = v11;
            v11_1 = 0;
            JSONObject resHeaders;
            boolean v5_3;
            String resp;
            boolean v6_1 = false;
            int v14 = 1;
            int v13 = 0;
            int requestResult;
            while (true) {
                if (retryCount >= 2) {
                    resHeaders = v9;
                    v5_3 = isPermanentNotify;
                    resp = null;
                    requestResult = v11_1;
                    break;
                }
                v12 = v11_1 + 1;
                ArticleQueryObj.RequestSession requestSession = new ArticleQueryObj.RequestSession();
                RequestParams requestParams = new RequestParams();
                requestParams.force_no_https = v14 == 0;
                requestParams.https_to_http = v15 ? 1 : 0;
                if (v14 != 0) {
                    v16 = 0;
                } else {
                    v16 = v14;
                }
                v11_2 = null;
                v32 = System.currentTimeMillis();
                if (v6_1) {
                    httpQueryHelper.put("strict", 1);
                }
                ResponseWrapper<String> responseWrapper;
                if (v19 != 0) {
                    responseWrapper = articleApi.postArticleList(-1, path, httpQueryHelper.queryMap, requestParams).execute();
                    AppData.getInstance().markFeedFirstLoad();
                    ++articleRecentAppSentCnt;
                    SharedPreferences.Editor editor = context.getSharedPreferences("misc_config", 0).edit();
                    editor.putInt("article_recent_app_sent_cnt", articleRecentAppSentCnt);
                    SharedPreferenceCompat.commit(editor);
                } else {
                    responseWrapper = articleApi.getArticleList(-1, path, httpQueryHelper.queryMap, requestParams).execute();
                    AppData.getInstance().markFeedFirstLoad();
                }
                host = responseWrapper.rawResponse.url;
                v14_2 = v11_2;
                int status = responseWrapper.rawResponse.status;
                String response = responseWrapper.body;
                v11 = host;
                if (!requestParams.using_https) {
                    ++v13;
                }
                String contentType = "";
                if (status != 200) {
                    v8_2 = v9;
                } else if (!requestParams.using_https) {
                    if (requestParams.headers == null) {
                        requestParams.headers = new JSONObject();
                    }
                    v9_1 = requestParams.headers.optString("CONTENT-TYPE");
                    String rawSign = requestParams.headers.optString("X-SS-SIGN");
                    if (android.support.design.a.isTextEmpty(rawSign)) {
                        rawSign = "";
                    }
                    requestSession.contentHijack = !RequestValidator.checkSign(fullUrl, rawSign, response, requestParams);
                    requestSession.rawSign = rawSign;
                    requestSession.ssSign = requestParams.ss_sign;
                    requestSession.localSign = requestParams.local_sign;
                    requestSession.bodyIsJson = requestParams.body_is_json;
                    requestSession.decodeTime = requestParams.decodeTime;
                    requestParams.headers.put("raw_sign", rawSign);
                    requestParams.headers.put("ss_sign", requestParams.ss_sign);
                    requestParams.headers.put("local_sign", requestParams.local_sign);
                    requestParams.headers.put("body_is_json", requestParams.body_is_json);
                    requestParams.headers.put("is_strict", v6_1);
                    v8_2 = requestParams.headers;
                    contentType = v9_1;
                } else {
                    v8_2 = v9;
                }

                requestSession.is2g = NetworkUtils.is2g(context);
                requestSession.https2http = AppConfig.https2http();
                requestSession.httpRetry = AppConfig.httpRetry();
                requestSession.showHijack = AppConfig.showHijack();
                requestSession.httpQuestTime = v13;
                requestSession.reqTime = System.currentTimeMillis() - v32;
                requestSession.status = status;
                requestSession.isStrict = v6_1;
                requestSession.httpsFailTimes = requestParams.https_fail_times;
                requestSession.usingHttps = requestParams.using_https;
                requestSession.remoteIp = requestParams.remoteIp;
                articleQueryObj.mStreamReqStatParamsAssembler.requestSessionLinkedList.add(requestSession);
                if (!requestParams.using_https
                        && (requestSession.contentHijack)
                        && !v6_1
                        && !contentType.contains("html")) {
                    RequestValidator.a(fullUrl, response, v8_2, null);
                }

                if (!requestParams.using_https) {
                    if (requestSession.status == 200) {
                        if (!requestSession.contentHijack) {
                            requestResult = 1;
                        } else if (v13 < 2 && (AppConfig.httpRetry()) && !NetworkUtils.is2g(context)) {
                            requestResult = 2;
                        } else if (!AppConfig.showHijack()) {
                            requestResult = 4;
                        } else {
                            requestResult = 1;
                        }
                    } else {
                        requestResult = 3;
                    }
                } else if (requestSession.status == 200) {
                    requestResult = 1;
                } else {
                    if ((AppConfig.https2http())
                            && retryCount == 0
                            && requestSession.status < 200) {
                        requestResult = 2;
                    } else {
                        requestResult = 3;
                    }
                }

                if (requestResult != 2) {
                    if (requestResult == 4) {
                        articleQueryObj.finishRequestTimeStamp = System.currentTimeMillis();
                        articleQueryObj.netRequestTime = articleQueryObj.finishRequestTimeStamp - articleQueryObj.startRequestTimeStamp;
                        articleQueryObj.errorCode = 23;
                        return false;
                    }
                    if (requestResult == 1) {
                        resHeaders = v8_2;
                        v5_3 = requestSession.contentHijack;
                        resp = response;
                        requestResult = v12;
                        host = v11;
                    } else {
                        throw v14_2;
                    }
                } else {
                    v14_3 = requestParams.using_https;
                    if (RequestValidator.isFeed(fullUrl)) {
                        isPermanentNotify = true;
                    } else {
                        isPermanentNotify = v6_1;
                    }
                    ++retryCount;
                    v15 = v14_3;
                    v14 = v16;
                    v6_1 = isPermanentNotify;
                    isPermanentNotify = requestSession.contentHijack;
                    v9 = v8_2;
                    host = v11;
                    v11_1 = v12;
                    continue;
                }
                break;
            }
            articleQueryObj.finishRequestTimeStamp = System.currentTimeMillis();
            articleQueryObj.netRequestTime = articleQueryObj.finishRequestTimeStamp - articleQueryObj.startRequestTimeStamp;
            articleQueryObj.errorOccur = requestResult > 1;
            if (!android.support.design.a.isTextEmpty(resp)) {
                JSONObject responseJO = new JSONObject(resp);
                if (!"success".equals(responseJO.getString("message"))) {
                    articleQueryObj.exceptionMsg = "error response: " + resp;
                    Logger.d("ArticleQueryThread", "get article list error: " + resp);
                    articleQueryObj.errorCode = 17;
                    articleQueryObj.cid = 1;
                    return false;
                }

                articleQueryObj.hasMoreToRefresh = "__all__".equals(articleQueryObj.category)
                        ? optBoolean(responseJO, "has_more_to_refresh", true)
                        : optBoolean(responseJO, "has_more_to_refresh", false);
                articleQueryObj.loginStatus = responseJO.optInt("login_status", 0) > 0;
                articleQueryObj.hasMore = getHasMore(responseJO, false);
                responseJO.optInt("total_count", 0);
                articleQueryObj.feedFlag = responseJO.optInt("feed_flag", 0);
                optBoolean(responseJO, "show_top_pgc_list", false);
                articleQueryObj.actionToLastStick = responseJO.optInt("action_to_last_stick", 0);
                articleQueryObj.showEtStatus = responseJO.optInt("show_et_status");
                articleQueryObj.postContentHint = responseJO.optString("post_content_hint");
                articleQueryObj.respOffset = responseJO.optInt("offset");
                AppData appData = AppData.getInstance();
                String userCity = articleQueryObj.userCity;
                int feedFlag = articleQueryObj.feedFlag;
                if (appData.mContext != null
                        && !android.support.design.a.isTextEmpty(userCity)
                        && feedFlag >= 0) {
                    SharedPreferences.Editor editor = appData.mContext.getSharedPreferences("app_setting", 0).edit();
                    editor.putInt(userCity, feedFlag);
                    SharedPreferenceCompat.commit(editor);
                }
                parseSubEntranceList(context, articleQueryObj, responseJO);
                articleQueryObj.responseStartParseTs = System.currentTimeMillis();
                JSONArray responseDataJA = responseJO.getJSONArray("data");
                int dataLength = responseDataJA.length();
                ArrayList<CellRef> cellRefList = new ArrayList();
                String category = "";
                String categoryName;
                if (articleQueryObj.categoryType == 1) {
                    category = articleQueryObj.category;
                    if (!android.support.design.a.isTextEmpty(responseJO.optString("category_name"))) {
                        category = responseJO.optString("category_name");
                    }

                    if ("news_local".equals(category)) {
                        category = articleQueryObj.category + "_" + articleQueryObj.userCity;
                    }

                    if (v22 != 0) {
                        categoryName = "__widget__";
                    } else if (v21 == 0) {
                        categoryName = category;
                    } else {
                        categoryName = "__permanent__";
                    }
                } else {
                    if (articleQueryObj.categoryType == 3) {
                        categoryName = "__search__";
                    } else if (articleQueryObj.categoryType == 4) {
                        categoryName = "__pgc__";
                    } else if (articleQueryObj.categoryType != 7) {
                        categoryName = category;
                    } else {
                        categoryName = articleQueryObj.category;
                    }
                }

                boolean isCategoryType2 = articleQueryObj.categoryType == 2;
                if (isCategoryType2 && dataLength <= 0) {
                    articleQueryObj.hasMore = false;
                }

                v13 = !v5_3 ? 1 : 0;
                JSONArray decodeTimes = new JSONArray();
                JSONArray hijackCellIndexes = new JSONArray();
                if (articleQueryObj.categoryType == 8 || articleQueryObj.categoryType == 9) {
                    for (int i = 0; i < dataLength; ++i) {
                        JSONObject responseDataJO = responseDataJA.getJSONObject(i);
                        long date = responseDataJO.getLong("date");
                        long count = responseDataJO.getLong("count");
                        String headText = responseDataJO.getString("head_text");
                        JSONArray dataJA = responseDataJO.getJSONArray("data");
                        if (dataJA != null) {
                            int length = dataJA.length();
                            for (int j = 0; j < length; ++j) {
                                JSONObject jo = dataJA.getJSONObject(j);
                                int cellType = jo.optInt("cell_type", -1);
                                if (cellType == -1) {
                                    int isAd = jo.optInt("is_ad", 0) > 0 ? 1 : 0;
                                    if (isAd != 0) {
                                        cellType = 1;
                                    } else {
                                        cellType = 0;
                                    }
                                }
                                long behotTime = jo.optLong("behot_time");
                                if (behotTime > 0) {
                                    long cateCursor = jo.optLong("cursor");
                                    CellRef cellRef = new CellRef(cellType, categoryName, behotTime);
                                    if (CellRef.extractArticle(cellRef, jo)) {
                                        cellRef.cateCursor = cateCursor;
                                        if ("read".equals(articleQueryObj.historyType)) {
                                            cellRef.readDate = date;
                                            cellRef.readCountcount = count;
                                            cellRef.readHeadText = headText;
                                        } else if ("push".equals(articleQueryObj.historyType)) {
                                            cellRef.pushDate = date;
                                            cellRef.pushCount = count;
                                            cellRef.pushHeadText = headText;
                                        }
                                        cellRefList.add(cellRef);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    v7_1 = 0;
                    while (true) {
                        if (v7_1 < dataLength) {
                            JSONObject jo = responseDataJA.getJSONObject(v7_1);
                            if (RequestValidator.isFeed(fullUrl)) {
                                if (v13 == 0 && (v6_1)) {
                                    long beforeValidTs = System.currentTimeMillis();
                                    boolean isContentValid = RequestValidator.validContent(jo);
                                    decodeTimes.put(System.currentTimeMillis() - beforeValidTs);
                                    if (!isContentValid) {
                                        hijackCellIndexes.put(v7_1);
                                        ++v7_1;
                                        continue;
                                    }
                                }
                                jo = new JSONObject(jo.optString("content"));
                            } else {
                                if (!TextUtils.isEmpty(fullUrl)
                                        && (fullUrl.contains("/wenda/v1/"))) {
                                    jo = new JSONObject(jo.optString("content"));
                                }
                            }
                            int cellType = jo.optInt("cell_type", -1);
                            JSONObject logPbJO = jo.optJSONObject("log_pb");
                            int videoChannelAdType = jo.optInt("video_channel_ad_type", 0);
                            if (cellType == -1) {
                                int isAd = jo.optInt("is_ad", 0) > 0 ? 1 : 0;
                                if (isAd != 0) {
                                    cellType = 1;
                                } else {
                                    cellType = 0;
                                }
                            }
                            long behotTime = jo.optLong("behot_time");
                            if (behotTime <= 0) {
                                ++v7_1;
                                continue;
                            }

                            v32 = jo.optLong("cursor");
                            CellRef cellRef = null;
                            CellRef v4_13;
                            if (cellType == 0) {
                                cellRef = new CellRef(cellType, categoryName, behotTime);
                                cellRef.videoChannelAdType = videoChannelAdType;
                                if (CellRef.extractArticle(cellRef, jo)) {
                                    if (isCategoryType2 && cellRef.article != null) {
                                        Article article = cellRef.article;
                                        article.userRepin = jo.optInt("user_repin", 0) == 1;
                                        long userRepinTime = jo.optLong("user_repin_time", 0);
                                        if (userRepinTime > 0) {
                                            cellRef.article.userRepinTime = userRepinTime;
                                        }
                                        v4_13 = cellRef;
                                    } else {
                                        v4_13 = cellRef;
                                    }
                                } else {
                                    ++v7_1;
                                    continue;
                                }
                            } else {
                                if (cellType == 10) {
                                    cellRef = new CellRef(cellType, categoryName, behotTime);
                                    cellRef.videoChannelAdType = videoChannelAdType;
                                    if (!CellRef.extractAppAd(cellRef, jo, true)) {
                                        ++v7_1;
                                        continue;
                                    }
                                    CellRef.extractFromJO(cellRef, jo, true);
                                    v4_13 = cellRef;
                                } else if (cellType == 16) {
                                    cellRef = new CellRef(cellType, categoryName, behotTime);
                                    if (!CellRef.extractActionAd(cellRef, jo, true)) {
                                        ++v7_1;
                                        continue;
                                    }

                                    CellRef.extractFromJO(cellRef, jo, true);
                                    v4_13 = cellRef;
                                } else if (cellType == 30) {
                                    cellRef = new CellRef(cellType, categoryName, behotTime);
                                    if (!CellRef.extractLbsAd(cellRef, jo, true)) {
                                        ++v7_1;
                                        continue;
                                    }
                                    v4_13 = cellRef;
                                } else if (cellType != 36) {
                                    v4_13 = cellRef;
                                } else {
                                    cellRef = new CellRef(cellType, categoryName, behotTime);
                                    if (!CellRef.extractWendaEntity(cellRef, jo)) {
                                        ++v7_1;
                                        continue;
                                    }
                                    CellRef.extractFromJO(cellRef, jo, true);
                                    v4_13 = cellRef;
                                }
                            }
                            if (v4_13 != null) {
                                v4_13.cateCursor = v32;
                                v4_13.logPb = logPbJO;
                                ((List) cellRefList).add(v4_13);
                            }
                            ++v7_1;
                            continue;
                        }
                        break;
                    }
                }
                articleQueryObj.responseFinishParseTs = System.currentTimeMillis();
                articleQueryObj.responseParseCost = articleQueryObj.responseFinishParseTs - articleQueryObj.responseStartParseTs;

                if (v6_1) {
                    JSONObject jo = new JSONObject();
                    jo.put("parse_time", System.currentTimeMillis() - parseStartTime);
                    if (decodeTimes.length() > 0) {
                        jo.put("decode_times", decodeTimes);
                    }
                    long v8_5 = v13 != 0 ? 0 : 1;
                    MobClickCombiner.onEvent(context, "strict_http_stat", "feed", v8_5, hijackCellIndexes.length(), jo);
                    if (v13 == 0) {
                        RequestValidator.a(fullUrl, resp, resHeaders, hijackCellIndexes);
                    }
                }
                if (articleQueryObj.isRefreshing) {
                    Iterator<CellRef> iterator = cellRefList.iterator();
                    while (iterator.hasNext()) {
                        CellRef cellRef = iterator.next();
                        if (cellRef.stickStyle <= 0) {
                            continue;
                        }
                        if (articleQueryObj.s == null) {
                            articleQueryObj.s = new ArrayList();
                        }
                        articleQueryObj.s.add(cellRef);
                    }
                }
                boolean[] v13_1 = new boolean[1];
                if (articleQueryObj.categoryType != 2) {
                    if (articleQueryObj.categoryType != 1
                            && (articleQueryObj.categoryType != 7 || articleQueryObj.refer != 0)) {
                        if (articleQueryObj.categoryType != 3
                                && articleQueryObj.categoryType != 4) {

                        } else if (cellRefList.isEmpty()) {

                        } else {
                            dbHelper.insertArticleList(cellRefList);
                        }
                    } else {
                        if (!cellRefList.isEmpty()) {
                            long[] behotTimeRange = new long[]{cellRefList.get(0).cateBehotTime,
                                    cellRefList.get(cellRefList.size() - 1).cateBehotTime};
                            long firstCateBehotTime = behotTimeRange[0];
                            long lastCateBehotTime = behotTimeRange[1];
                            if (articleQueryObj.minBehotTime > 0) {
                                if (!articleQueryObj.hasMore && articleQueryObj.minBehotTime < lastCateBehotTime) {
                                    lastCateBehotTime = articleQueryObj.minBehotTime;
                                }
                            } else if (articleQueryObj.maxBehotTime > 0 && articleQueryObj.maxBehotTime > firstCateBehotTime) {
                                firstCateBehotTime = articleQueryObj.maxBehotTime;
                            }

                            articleQueryObj.firstCateBehotTime = behotTimeRange[0];
                            articleQueryObj.lastCateBehotTime = behotTimeRange[1];
                            boolean isRefresh = articleQueryObj.minBehotTime <= 0 && articleQueryObj.maxBehotTime <= 0;
                            dbHelper.deleteOrCancelLastStickData(articleQueryObj);
                            long updateDbStartTs = System.currentTimeMillis();
                            articleQueryObj.updateDbStartTs = updateDbStartTs;
                            dbHelper.updateCategoryRange(cellRefList, categoryName, firstCateBehotTime, lastCateBehotTime, isRefresh, v13_1);
                            articleQueryObj.updateDbEndTs = System.currentTimeMillis();
                            articleQueryObj.updateDbCost = articleQueryObj.updateDbEndTs - updateDbStartTs;
                        } else {
                            dbHelper.updateCategoryRefreshTime(categoryName);
                        }
                        isPermanentNotify = AppData.getInstance().D();
                        if (isPermanentNotify && articleQueryObj.isRefreshing && articleQueryObj.refer == 1) {
                            int showEtStatus = articleQueryObj.showEtStatus;
                            String postContentHint = articleQueryObj.postContentHint;
                            String[] whereArgs = new String[]{categoryName};
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("show_et_status", Integer.valueOf(showEtStatus));
                            contentValues.put("post_content_hint", postContentHint);
                            contentValues.put("refresh_time", Long.valueOf(System.currentTimeMillis()));
                            if (dbHelper.mSQLiteDatabase.update(
                                    "v38_category_meta",
                                    contentValues,
                                    "category_name=?",
                                    whereArgs) <= 0) {
                                contentValues.put("category_name", categoryName);
                                dbHelper.mSQLiteDatabase.insert("v38_category_meta", null, contentValues);
                            }
                        } else if (!isPermanentNotify) {
                        } else if (!articleQueryObj.isRefreshing) {
                        } else if (articleQueryObj.refer != 2) {
                        } else {
                            long concernId = articleQueryObj.E;
                            int showEtStatus = articleQueryObj.showEtStatus;
                            String postContentHint = articleQueryObj.postContentHint;
                            String[] whereArgs = new String[]{String.valueOf(concernId)};
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("concern_info", (String) null);
                            contentValues.put("show_et_status", Integer.valueOf(showEtStatus));
                            contentValues.put("post_content_hint", postContentHint);
                            contentValues.put("refresh_time", Long.valueOf(System.currentTimeMillis()));
                            if (dbHelper.mSQLiteDatabase.update("v38_category_meta",
                                    contentValues,
                                    "concern_id=?",
                                    whereArgs) > 0) {
                            } else {
                                contentValues.put("concern_id", Long.valueOf(concernId));
                                dbHelper.mSQLiteDatabase.insert("v38_category_meta", null, contentValues);
                            }
                        }
                    }
                } else {
                    if (!cellRefList.isEmpty()) {
                        long v6_2 = 0;
                        ArrayList v8_6 = new ArrayList();
                        Iterator<CellRef> iterator = cellRefList.iterator();
                        while (true) {
                            if (iterator.hasNext()) {
                                long concernId;
                                CellRef cellRef = iterator.next();
                                if (cellRef.cellType != 0) {
                                    concernId = v6_2;
                                } else if (cellRef.article != null) {
                                    ((List) v8_6).add(cellRef.article);
                                    if (v6_2 > 0 && v6_2 <= cellRef.article.userRepinTime) {
                                        concernId = v6_2;
                                    } else {
                                        concernId = cellRef.article.userRepinTime;
                                    }
                                } else {
                                    concernId = v6_2;
                                }
                                v6_2 = concernId;
                                continue;
                            } else {
                                if (!cellRefList.isEmpty()) {
                                    dbHelper.insertFavorList(v8_6);
                                }
                                articleQueryObj.lastCateBehotTime = v6_2;
                                break;
                            }
                        }
                    }
                    int limit = articleQueryObj.hasMore ? articleQueryObj.count : 5000;
                    if ((cellRefList).isEmpty() || ((cellRefList).size() == 0)) {
                        cellRefList = (ArrayList) dbHelper.queryFavor(articleQueryObj.l, limit);
                    }
                }
                JSONObject tipsJO = responseJO.optJSONObject("tips");
                if (tipsJO != null) {
                    AdsAppItem adsAppItem = AdsAppItem.fromJO(tipsJO);
                    if (adsAppItem != null) {
                        adsAppItem.k = v13_1[0];
                    }
                    articleQueryObj.mAdsAppItem = adsAppItem;
                }
                articleQueryObj.q = cellRefList;
                a.set(0);
                return true;
            } else {
                articleQueryObj.exceptionMsg = "response is null.";
                articleQueryObj.errorCode = 17;
                articleQueryObj.cid = 1;
                return false;
            }
        } catch (Throwable throwable) {
            Logger.w("DBHelper", "insert or update category meta exception: " + throwable);
            int errorCode = TTUtils.getErrorCode(context, throwable);
            articleQueryObj.errorIp = null;
            articleQueryObj.exceptionMsg = throwable.getMessage();
            articleQueryObj.exception = throwable.toString();
            if (errorCode == 18 && (Logger.debug())) {
                throwable.printStackTrace();
            }
            if (useOkHttp != 0) {
                if ((throwable instanceof EOFException)) {
                    c.incrementAndGet();
                    if (c.get() < 2) {
                        v6_4 = 1;
                    } else {
                        v6_4 = 0;
                    }
                } else {
                    v6_4 = 0;
                }
            } else {
                v6_4 = 0;
            }

            v7_1 = b;
            if (v7_1 < 0) {
                v7_1 = 3;
            }

            if (v7_1 > 0 && (AppUtil.a(errorCode)) && a.incrementAndGet() % v7_1 == 0) {
                v6_4 = 1;
            }

            if (v6_4 == 0) {
                articleQueryObj.errorCode = errorCode;
                articleQueryObj.cid = AppLog.w();
                return false;
            }
            AppLog.z();
            articleQueryObj.errorCode = errorCode;
            articleQueryObj.cid = AppLog.w();
            return false;
        }


    }

    public static void b(List arg0) {
        g = arg0;
    }

    public final boolean needTryLocal() {
        boolean v0 = true;
        if (this.mArticleQueryObj.categoryType == 1 || this.mArticleQueryObj.refer == 0) {
            if (!this.mArticleQueryObj.d && !this.mArticleQueryObj.j) {
                if (this.mArticleQueryObj.categoryType != 2) {
                    v0 = false;
                }
            }
        } else if (this.mArticleQueryObj.categoryType != 7) {
            if (this.mArticleQueryObj.categoryType != 2) {
                v0 = false;
            }
        } else {
            if (this.mArticleQueryObj.categoryType != 2) {
                v0 = false;
            }
        }

        return v0;
    }

    public final void run() {
        int v0_2;
        int v4;
        Object v0_1;
        String v3;
        if (this.mArticleQueryObj.O) {
            com.ss.android.newmedia.k.a();
        }

        this.mHandler.sendMessage(this.mHandler.obtainMessage(10012, this.mArticleQueryObj));
        boolean v9 = queryArticle(this.mContext, this.mArticleQueryObj, this.l);
        try {
            if (this.mArticleQueryObj.mStreamReqStatParamsAssembler == null) {

            } else {
                v3 = "__all__".equals(this.mArticleQueryObj.category) ? "feed" : "channel";
                v0_1 = this.mArticleQueryObj.mStreamReqStatParamsAssembler.requestSessionLinkedList.getLast();
                if (v0_1 == null) {
                    v4 = 2;
                } else if (((ArticleQueryObj.RequestSession) v0_1).status == 200) {
                    v4 = 1;
                } else {
                    v4 = 2;
                }

                if (v0_1 != null) {
                    v0_2 = ((ArticleQueryObj.RequestSession) v0_1).status;
                } else {
                    v0_2 = -2;
                }
                MobClickCombiner.onEvent(this.mContext, "stream_req_stat", v3, v4, v0_2, this.mArticleQueryObj.mStreamReqStatParamsAssembler.assembleParams());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        if (this.mArticleQueryObj.firstCateBehotTime > 0
                && this.mArticleQueryObj.firstCateBehotTime == this.mArticleQueryObj.lastCateBehotTime) {
            --this.mArticleQueryObj.lastCateBehotTime;
        }
        v0_2 = v9 ? 10 : 11;
        Message message = this.mHandler.obtainMessage(v0_2);
        message.obj = this.mArticleQueryObj;
        this.mHandler.sendMessage(message);
    }

    public final boolean run4Local() {
        long v0 = System.currentTimeMillis();
        boolean v2 = a(this.mContext, this.mArticleQueryObj);
        this.mArticleQueryObj.localLoadTime = System.currentTimeMillis() - v0;
        if (this.mArticleQueryObj.firstCateBehotTime > 0
                && this.mArticleQueryObj.firstCateBehotTime == this.mArticleQueryObj.lastCateBehotTime) {
            --this.mArticleQueryObj.lastCateBehotTime;
        }

        if (v2 || (this.mArticleQueryObj.d)) {
            int v0_1 = v2 ? 10 : 11;
            Message message = this.mHandler.obtainMessage(v0_1);
            message.obj = this.mArticleQueryObj;
            this.mHandler.sendMessage(message);
        }

        return v2;
    }
}

